/*
 * FilterableTableModel.java
 *
 * Created on August 30, 2007, 11:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.table;

import java.util.ArrayList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author bruce
 */
public class FilterableTableModel extends AbstractTableModel implements TableModelListener{
    private TableModel tableModel;
    private int [] rowMap;
    private int editingRow;
    private static final int ALL_NEWS = 0;
    private static final int UNREAD = 1;
    private static final int STARRED = 2;
    private int mode = 0;
    private boolean modeChanged = false;
    /** Creates a new instance of FilterableTableModel */
    public FilterableTableModel(TableModel tableModel) {
        this.tableModel = tableModel;
        tableModel.addTableModelListener(this);
    }
    
    
    public boolean isCellEditable(int row,int col) {
        return tableModel.isCellEditable(row,col);
    }
    
    public int getRowCount() {
        if(rowMap == null)
            return 0;
        return  rowMap.length;
    }
    
    public int getColumnCount() {
        return tableModel.getColumnCount();
    }
    
    public Class getColumnClass(int columnIndex) {
        return tableModel.getColumnClass(columnIndex);
    }
    
    public String getColumnName(int column) {
        return tableModel.getColumnName(column);
    }
    
    public Object getValueAt(int row, int col) {
        return tableModel.getValueAt(getOriginalRow(row),col);
    }
    
    public void setValueAt(Object value,int row,int col) {
        editingRow = row;
        tableModel.setValueAt(value,getOriginalRow(row),col);
    }
    
    public int getOriginalRow(int row) {
        return rowMap[row];
    }
    
    public void tableChanged(TableModelEvent e) {
        /**if e == null that means the whole table changed*/
        if(e == null) {
            rowMap = new int[tableModel.getRowCount()];
            for (int i = 0; i < rowMap.length; i++) {
                rowMap[i] = i;
            }            
            fireTableChanged(null);
        } else if(e.getType() != e.ALL_COLUMNS && e.getFirstRow() == e.getLastRow() && e.getType() == e.UPDATE) {
            fireTableCellUpdated(editingRow,0);
        } else if(e.getType() == e.UPDATE){
            fireTableChanged(null);
        }
    }
    
    public void setMode(int mode) {
        this.mode = mode;
        modeChanged = true;
        if(mode == UNREAD){
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                Boolean isRead = (Boolean) tableModel.getValueAt(i,0);
                if(!isRead) {
                    list.add(i);
                }
            }
            rowMap = new int[list.size()];
            for (int i = 0; i < rowMap.length; i++) {
                rowMap[i] = list.get(i);
            }
        }else if(mode == ALL_NEWS){
            rowMap = new int[tableModel.getRowCount()];
            for (int i = 0; i < rowMap.length; i++) {
                rowMap[i] = i;
            }
        }
    }
}
